//
//  TableScrollCell.m
//  轮胎商城
//
//  Created by 张晋磊 on 2017/3/24.
//  Copyright © 2017年 张晋磊. All rights reserved.
//

#import "TableScrollCell.h"

@interface TableScrollCell ()<UIScrollViewDelegate>

@property (nonatomic,copy) MwyBlock block;
@property (nonatomic,assign) int i;
@property (nonatomic,strong) NSMutableArray *arrBt;

@end

@implementation TableScrollCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
    
        _i=0;
    }
    return self;
}




-(void)timerWithImg{

    NSInteger index = _scrl.contentOffset.x/_scrl.frame.size.width;
    
    if(index == _arr.count-1){
        
        index = -1;
    }
    
    [_scrl setContentOffset:CGPointMake((index + 1) * _scrl.frame.size.width, 0)];
}

-(void)addTimer{

    self.time = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerWithImg) userInfo:nil repeats:YES];
}

-(void)setArr:(NSArray *)arr{

    _arr = arr;
    [self.contentView addSubview:self.scrl];
    [self.contentView addSubview:self.smallScrol];
}


-(void)smallBtClick:(UIButton *)bt{

    NSInteger index = bt.tag - 1000;//
    [_scrl setContentOffset:CGPointMake(index * _scrl.frame.size.width, 0)];
    [self dealWithBorder:bt];
}


-(void)buttonClick:(UIButton *)bt{

    if(_block){
    
        _block(bt.tag);
    }
}

-(void)coputeButtonClick:(void (^)(NSInteger))block{

    if(_block != block){
    
        _block = block;
    }
}

//处理边界问题(适用于单数)
-(void)dealWithBorder:(UIButton *)bt{

    
    CGFloat pointBt = bt.center.x-_smallScrol.center.x;
    
    CGFloat maxPoint = _smallScrol.contentSize.width - _smallScrol.frame.size.width;//最大偏移量
    
    if(pointBt < 0){
    
        pointBt = 0;
    }
    if(pointBt > maxPoint){
    
        pointBt = maxPoint;
    }
    
    [_smallScrol setContentOffset:CGPointMake(pointBt, 0)];
    
    
}

-(UIScrollView *)smallScrol{

    if(!_smallScrol){
    
        CGSize size = [UIScreen mainScreen].bounds.size;
        _smallScrol = [[UIScrollView alloc]initWithFrame:CGRectMake(0, _scrl.frame.size.height + 3, size.width, 40)];
        
        _smallScrol.contentSize = CGSizeMake(_arr.count * _smallScrol.frame.size.width / 3, _smallScrol.frame.size.height);
        
       
        
        for(int i=0;i<_arr.count;i++){
            
            UIButton *bt = [UIButton buttonWithType:UIButtonTypeCustom];
            
            bt.frame = CGRectMake(i * _smallScrol.frame.size.width / 3, 0, _smallScrol.frame.size.width / 3-2, _smallScrol.frame.size.height);
            
            [bt setImage:[UIImage imageNamed:_arr[i]] forState:UIControlStateNormal];
            
            bt.tag = 1000+i;
            [bt addTarget:self action:@selector(smallBtClick:) forControlEvents:UIControlEventTouchUpInside];
            
            [_smallScrol addSubview:bt];
            
            [_arrBt addObject:bt];//将按钮放入数组
        }

    }
    return _smallScrol;
}


-(UIScrollView *)scrl{

    if(!_scrl){
    
        CGSize size = [UIScreen mainScreen].bounds.size;
        _scrl = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, size.width, 120)];
        _scrl.pagingEnabled = YES;
        _scrl.delegate = self;
        _arrBt = [NSMutableArray array];
        
        _scrl.contentSize = CGSizeMake(_arr.count * _scrl.frame.size.width, _scrl.frame.size.height);
        
        for(int i=0;i<_arr.count;i++){
            
            UIButton *bt = [[UIButton alloc]initWithFrame:CGRectMake(i * _scrl.frame.size.width, 0, _scrl.frame.size.width, _scrl.frame.size.height)];
            
            [bt setImage:[UIImage imageNamed:_arr[i]] forState:UIControlStateNormal];
            bt.tag = 100+i;
            [bt addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
            bt.imageView.contentMode = UIViewContentModeScaleToFill;
            [_scrl addSubview:bt];
        }
        
        [self addTimerWithView];
  
    }
    return _scrl;
}

//添加定时器
-(void)addTimerWithView{

    _time = [NSTimer scheduledTimerWithTimeInterval:1.5 target:self selector:@selector(timerWithImg) userInfo:nil repeats:YES];
}

//暂停
-(void)stopTimerWithView{

    [_time setFireDate:[NSDate distantFuture]];//暂停定时器
}

//开启
-(void)startTimerWithView{

    [_time setFireDate:[NSDate dateWithTimeInterval:1.5 sinceDate:[NSDate date]]];
}

//用户正在操作视图  暂停定时器
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{

    [self stopTimerWithView];
}

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    [self scrollViewDidEndDecelerating:scrollView];
}

//视图静止时  无人操作
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    
    if([scrollView isEqual:_scrl]){
    
        CGPoint scrPoint = scrollView.contentOffset;
        
        NSInteger index = scrPoint.x / scrollView.frame.size.width;
        
        for(UIButton *bt in _arrBt){
        
            if(bt.tag == 1000+index){
            
                bt.selected = YES;
                bt.alpha = 1.0;//选中状态按钮变亮
                [self dealWithBorder:bt];//处理边界
            }else{
            
                bt.selected = NO;
                bt.alpha = 0.2;//没被选中按钮变暗
            }
        }
    }
    
    //开启定时器
    [self startTimerWithView];
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{

    [self scrollViewDidEndDecelerating:scrollView];
}




@end
